###################################################
#                                                 #
#          PREPARA EL CONTROL PARA MOSAR          #
#      Sardina común y Anchoveta Centro-Sur       #         
#                                                 #
#                  Luis Cubillos                  #
#          Departamento de Oceanografía           #
#            Universidad de Concepción            #
#                                                 #
###################################################

sOutDir <-"/Users/luiscubillos/ADMwork/MOSAR/FishStock/Sben" 
aOutDir <-"/Users/luiscubillos/ADMwork/MOSAR/FishStock/Erin"
CtrDir <- "/Users/luiscubillos/ADMwork/MOSAR/FishStock/Ctrls/CrtMOSARbase.R"
source(CtrDir)
rownames(theta) <-c("logRo","Steepness","logM","logAvgR","prho","kappa S-R")
mylist <- c("Sardina","Anchoveta")
theta
what2do <- (guiDlgList(mylist))
if(what2do=="Sardina"){
	CtrDir <- "/Users/luiscubillos/ADMwork/MOSAR/FishStock/Ctrls/CrtMOSARSben.R"
   source(CtrDir)
	setwd(sOutDir)
	outfile <- "sben2010.ctl"
}
if(what2do=="Anchoveta"){
	CtrDir <- "/Users/luiscubillos/ADMwork/MOSAR/FishStock/Ctrls/CrtMOSARErin.R"
   source(CtrDir)
	setwd(aOutDir)
	outfile <- "Erin2010.ctl"    
}

write("#MOSARv1.0",outfile)
write("#npar",outfile,append=T)
write(npar,outfile,append=T)

write("#Parametros_que_controlan_la_estimacion",outfile,append=T)
write("#ival lb ub phz prior p1 p2",outfile,append=T)
write.table(as.matrix(theta),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#Numero de artes de pesca con datos de composicion por tallas",outfile,append=T)
write("#gear: 3 (1: pesqueria, 2: reclas, 3: pelaces)",outfile,append=T)
write(ngear,outfile,append=T)

write("#Opciones_para_selectividad:",outfile,append=T)
write("#1: logistica a estimar",outfile,append=T)
write("#2: logistica fija",outfile,append=T)
write.table(as.matrix(isel_type),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#age50%",outfile,append=T)
write.table(ahat,outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#stdage50%",outfile,append=T)
write.table(ghat,outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#phase_selec",outfile,append=T)
write.table(sel_phz,outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#Control para los prior de capturabilidad:",outfile,append=T)
write("#qs=4 (1: cpue, 2:reclas, 3: pelaces, 4: mph)",outfile,append=T)
write("#Numero de indices de abundancia:",outfile,append=T)
write(nints,outfile,append=T)
write("#Tipo de prior (1: uniform, 2: normal):",outfile,append=T)
write.table(as.matrix(q_prior),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)
write("#Valor (log(mu)):",outfile,append=T)
write.table(as.matrix(q_mu),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)
write("#SD (log(mu)):",outfile,append=T)
write.table(as.matrix(q_sd),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)
write("#Phases q:",outfile,append=T)
write.table(as.matrix(q_phz),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)


write("#CVsurveys:",outfile,append=T)
write.table(as.matrix(cv_surveys),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)
write("#nsizemultinomial:",outfile,append=T)
write.table(as.matrix(nsizemult),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#Otros aspectos:",outfile,append=T)
write("#1 -> verbose",outfile,append=T)
write("#2 -> recruitment model (1=beverton-holt, 2=rickers)",outfile,append=T)
write("#3 -> Autocorrelation recruitment model (0=no, 1=yes)",outfile,append=T)
write("#4 -> std in catch first phase",outfile,append=T)
write("#5 -> std in catch in last phase",outfile,append=T)
write("#6 -> mean fishing mortality rate to regularize the solution",outfile,append=T)
write("#7 -> standard deviation of mean F penalty in first phases",outfile,append=T)
write("#8 -> standard deviation of mean F penalty in last phase",outfile,append=T)
write.table(as.matrix(cntrl),outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)
write("#fin:enfc",outfile,append=T)
write(enfc,outfile,append=T)

#CONECTA CON EL REPORTE DE CONTROLES
#source(repControls)
